﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common.Net.Utilities;
using System.Linq.Expressions;

namespace Common.Net.DesignByContract
{
    public static class Require
    {
        public static void IsFalse(Expression<Func<bool>> condition)
        {
            TrueInternal(!condition.Compile()(), "Value {0} must be false", ExpressionFormatter.Format(condition));
        }

        public static void IsTrue(Expression<Func<bool>> condition)
        {
            TrueInternal(condition.Compile()(), "Value {0} must be true", ExpressionFormatter.Format(condition));
        }

        static void TrueInternal(bool condition, string format, params object[] args)
        {
            if (!condition)
                throw ContractViolationException(format, args);
        }

        static DesignContractViolationException ContractViolationException(string format, params object[] args)
        {
            return new DesignContractViolationException(string.Format(format, args));
        }
    }
}
